using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Text;
using RandomRat.Data;

namespace RandomRat {


	
	/// <summary>
	/// Summary description for QuickStart.
	/// </summary>
	public class QuickStart : System.Windows.Forms.Form	{
		QuickStartAction _action = QuickStartAction.CloseQuickStart;

		DataManager dataManager;
		CriteriaDataSet criteriaDataSet;

		Thread workerThread;
		RandomSetGenerator rndSetGen;
		ArrayList rndSets;
		SetArguments setArgs;
		bool setArgsValid = false;

		#region WINVARS

		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tabPage2;
		private System.Windows.Forms.TabPage tabPage3;
		private System.Windows.Forms.TabPage tabPage4;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.TextBox txtSets;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.GroupBox groupBox4;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.GroupBox groupBox5;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.GroupBox groupBox6;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.ListBox listRecentActions;
		private System.Windows.Forms.Button butGenNewSets;
		private System.Windows.Forms.Button butLoadFromFile;
		private System.Windows.Forms.Button butNewCriteria;
		private System.Windows.Forms.Button butNewCriteriaWizard;
		private System.Windows.Forms.Button butCloseQuickStart;
		private System.Windows.Forms.CheckBox chkShowOnStartUp;
		private System.Windows.Forms.Button butExitRandomRat;
		private System.Windows.Forms.Button butBack1;
		private System.Windows.Forms.Button butNext;
		private System.Windows.Forms.Button butBack3;
		private System.Windows.Forms.Button butPrint;
		private System.Windows.Forms.Button butPrintPreview;
		private System.Windows.Forms.Button butSaveAs;
		private System.Windows.Forms.Button butCopyToClipboard;
		private System.Windows.Forms.Button butExport;
		private System.Windows.Forms.ProgressBar progressBar1;
		private System.Windows.Forms.Button butAbort;
		private RandomRat.CriteriaSummaryControl criteriaSummaryControl1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.ComponentModel.IContainer components = null;

		#endregion

		public QuickStart(DataManager dataManager, Form1 form1) {
			InitializeComponent();
			
			this.dataManager = dataManager;
			this.criteriaDataSet = dataManager.CriteriaDataSet;
			rndSetGen = new RandomSetGenerator();
			
			criteriaSummaryControl1.LoadData(dataManager);
			criteriaSummaryControl1.SetForm1(form1);

			butGenNewSets.Enabled = criteriaSummaryControl1.listBox1.Items.Count != 0;

			chkShowOnStartUp.Checked = ApplicationSettings.ShowQuickStart;

			fillActionList();
		}

		public QuickStartAction Action {
			get { return _action; }
			set { _action = value; }
		}

		public ArrayList RndSetsGenerated {
			get { return rndSets; }
		}


		private ArrayList getActionList() {
			ArrayList actionList = new ArrayList(10);
			DataTable actions = dataManager.CriteriaDataSet.Tables["Actions"];

			foreach (DataRow action in actions.Rows) {
				actionList.Add(new QuickStartActionItem((QuickStartActionSource)action["Source"], 
					(string)action["CriteriaID"], 
					(int)action["Sets"], 
					(QuickStartSetAction)action["Action"]));
			}

			return actionList;
		}

		public void fillActionList() {
			listRecentActions.DataSource = getActionList();
			listRecentActions.Enabled = listRecentActions.Items.Count > 0;
		}

		public void saveActionList(ArrayList actionList) { 
			DataTable actions = dataManager.CriteriaDataSet.Tables["Actions"]; 

			actions.Clear();

			foreach (QuickStartActionItem action in actionList) {
				actions.Rows.Add(new object[4] {(object)action.Source, (object)action.CriteriaID, (object)action.Sets, (object)action.Action} );
			}
		}

		public void addActionItem(QuickStartActionItem actionItem) {
			ArrayList actionList = getActionList();

			foreach (QuickStartActionItem action in actionList) {
				if (action.ToString().CompareTo(actionItem.ToString()) == 0) {
					// Send to top
					actionList.Remove(action);
					actionList.Insert(0, actionItem);
					return;
				}
			}
			
			
			if (actionList.Count >= 7) {
				actionList.RemoveAt(actionList.Count - 1);
				actionList.Insert(0, actionItem);
			} else {
				actionList.Insert(0, actionItem);
			}

			saveActionList(actionList);
		}


		private void butNext_Click(object sender, System.EventArgs e) {
			generateSets();	
		}


		string criteriaID;

		private CriteriaFormState formState;

        private void criteriaSummaryControl1_Action(object sender, RandomRat.UserActionArgs e)
        {
			if (e.UserAction == UserAction.SelectionChanged) {
				criteriaID = e.CriteriaID;
				formState = e.FormState;
				setArgs = e.FormState.ToSetArguments();
				setArgsValid = SetArguments.ValidateSetArgs(setArgs);

				txtSets.Text = criteriaSummaryControl1.SelectedFormState.Sets;

				if (!setArgsValid) {
					butNext.Enabled = false;
					label1.Visible = true;
				} else {
					butNext.Enabled = true;
					label1.Visible = false;
				}

			} else if (e.UserAction == UserAction.SelectionMade) {
				generateSets();	

			} else if (e.UserAction == UserAction.DeletedAllCriteria) {
				butNext.Enabled = false;
				txtSets.Enabled = false;
			}
		}


		private void txtSets_Leave(object sender, System.EventArgs e) {
			int curIndx = criteriaSummaryControl1.listBox1.SelectedIndex;
			((DataRowView)criteriaSummaryControl1.listBox1.SelectedItem).Row["Sets"] = txtSets.Text;
			criteriaSummaryControl1.RefreshView();
			criteriaSummaryControl1.listBox1.SelectedIndex = curIndx;
		}
		
		private void generateSets() {			
			progressBar1.Value = progressBar1.Minimum;
			
			// Validate input
			if (!setArgsValid) {
				return;
			}

			if (txtSets.Text.Trim() == "") {
				MessageBox.Show(this, "Please provide a value for 'Sets to generate.'");
				txtSets.Focus();
				txtSets.SelectAll();
				return;
			}
			int setsToGen = int.Parse(txtSets.Text);
			if (setsToGen < 1) {
				MessageBox.Show(this, "'Sets to generate' must be greater than 0.");
				txtSets.Focus();
				txtSets.SelectAll();
				return;
			}

			// Nav to next page
			label2.Text = "Generating sets...";
			tabControl1.SelectedIndex++;
			
			workerThread = new Thread(new ThreadStart(this.DoWork));
			workerThread.Start();
		}

       
		private void DoWork() {

            this.Invoke(new UIWork(preGenRndSetsUIWork));

            rndSets = genRndSets();

            this.Invoke(new UIWork(postGenRndSetsUIWork));
		}

		
		public void DoActionItem(QuickStartActionItem actionItem) {
			if (actionItem.Source == QuickStartActionSource.Generate) {

				setArgs = dataManager.GetCriteriaFormState(actionItem.CriteriaID).ToSetArguments();
				if (!SetArguments.ValidateSetArgs(setArgs)) {
					MessageBox.Show(this, "The criteria selected is no longer valid");
					return;
				} else {
					label2.Text = "Generating sets...";
					tabControl1.SelectedIndex = 2;
			
					this.actionItem = actionItem;
					txtSets.Text = actionItem.Sets.ToString();
					criteriaID = actionItem.CriteriaID;
		
					workerThread = new Thread(new ThreadStart(this.DoWorkOnActionItem));
					workerThread.Start();
				}
			}
		}

		
		private QuickStartActionItem actionItem = null;

        
        private void preGenRndSetsUIWork()
        {
            butPrint.Enabled = false;
            butPrintPreview.Enabled = false;
            butSaveAs.Enabled = false;
            butCopyToClipboard.Enabled = false;
            butBack3.Enabled = false;

            butAbort.Enabled = true;
        }

        private void postGenRndSetsUIWork()
        {
            butAbort.Enabled = false;

            label2.Text = "Select an action";
            butPrint.Enabled = true;
            butPrintPreview.Enabled = true;
            butSaveAs.Enabled = true;
            butCopyToClipboard.Enabled = true;
            butBack3.Enabled = true;

        }

        private void performAction()
        {
            if (lastMyActionItem != null)
            {
                if (lastMyActionItem.Action == QuickStartSetAction.Print)
                {
                    printSets();
                }
                else if (lastMyActionItem.Action == QuickStartSetAction.PrintPreview)
                {
                    printPreviewSets();
                }
                else if (lastMyActionItem.Action == QuickStartSetAction.SaveSetsAs)
                {
                    saveSetsAs();
                }
                else if (lastMyActionItem.Action == QuickStartSetAction.CopyToClipboard)
                {
                    copyToClip();
                }
            }
        }

        QuickStartActionItem lastMyActionItem;

		private void DoWorkOnActionItem() {
            lastMyActionItem = actionItem;
			this.actionItem = null;

            this.Invoke(new UIWork(preGenRndSetsUIWork));

			rndSets = genRndSets();
            
            this.Invoke(new UIWork(postGenRndSetsUIWork));

            this.Invoke(new UIWork(performAction));

		}

		
		private void butAbort_Click(object sender, System.EventArgs e) {
			if (workerThread != null && workerThread.IsAlive) {
				workerThread.Abort();
				label2.Text = "Set generation aborted";
			}
			butAbort.Enabled = false;
			butBack3.Enabled = true;
		}

        int lastSetsToGen = -1;
        private void genRndSets_progUIInit()
        {
            lastSetsToGen = int.Parse(txtSets.Text);
            progressBar1.Minimum = 1;
            progressBar1.Maximum = lastSetsToGen + 1;
            progressBar1.Value = 1;
        }

        private void genRndSets_progUIInc()
        {
            progressBar1.Value += 1;
        }

		// Called from doWork (via the WorkingThread)
		// Generates n rndSets while updating the progress bar
		private ArrayList genRndSets() {
			rndSets = new ArrayList();

            this.Invoke(new UIWork(genRndSets_progUIInit));
			
			int i;
            for (i = 0; i < lastSetsToGen; i++)
            {
				rndSets.Add(rndSetGen.GetValidList(setArgs));

                this.Invoke(new UIWork(genRndSets_progUIInc));
			}

			return rndSets;
		}


		


		#region UTIL
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(QuickStart));
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tabPage2 = new System.Windows.Forms.TabPage();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.label3 = new System.Windows.Forms.Label();
			this.groupBox4 = new System.Windows.Forms.GroupBox();
			this.listRecentActions = new System.Windows.Forms.ListBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.butNewCriteriaWizard = new System.Windows.Forms.Button();
			this.butNewCriteria = new System.Windows.Forms.Button();
			this.butLoadFromFile = new System.Windows.Forms.Button();
			this.butGenNewSets = new System.Windows.Forms.Button();
			this.tabPage3 = new System.Windows.Forms.TabPage();
			this.label1 = new System.Windows.Forms.Label();
			this.criteriaSummaryControl1 = new RandomRat.CriteriaSummaryControl();
			this.butNext = new System.Windows.Forms.Button();
			this.butBack1 = new System.Windows.Forms.Button();
			this.groupBox6 = new System.Windows.Forms.GroupBox();
			this.label5 = new System.Windows.Forms.Label();
			this.txtSets = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.tabPage4 = new System.Windows.Forms.TabPage();
			this.butAbort = new System.Windows.Forms.Button();
			this.progressBar1 = new System.Windows.Forms.ProgressBar();
			this.butBack3 = new System.Windows.Forms.Button();
			this.groupBox5 = new System.Windows.Forms.GroupBox();
			this.label2 = new System.Windows.Forms.Label();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.butExport = new System.Windows.Forms.Button();
			this.butCopyToClipboard = new System.Windows.Forms.Button();
			this.butSaveAs = new System.Windows.Forms.Button();
			this.butPrintPreview = new System.Windows.Forms.Button();
			this.butPrint = new System.Windows.Forms.Button();
			this.butCloseQuickStart = new System.Windows.Forms.Button();
			this.chkShowOnStartUp = new System.Windows.Forms.CheckBox();
			this.butExitRandomRat = new System.Windows.Forms.Button();
			this.tabControl1.SuspendLayout();
			this.tabPage2.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox4.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.tabPage3.SuspendLayout();
			this.groupBox6.SuspendLayout();
			this.tabPage4.SuspendLayout();
			this.groupBox5.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// tabControl1
			// 
			this.tabControl1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					  this.tabPage2,
																					  this.tabPage3,
																					  this.tabPage4});
			this.tabControl1.Location = new System.Drawing.Point(-4, -24);
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(420, 348);
			this.tabControl1.TabIndex = 0;
			this.tabControl1.TabStop = false;
			this.tabControl1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tabControl1_KeyDown);
			this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
			// 
			// tabPage2
			// 
			this.tabPage2.Controls.AddRange(new System.Windows.Forms.Control[] {
																				   this.groupBox2,
																				   this.groupBox4,
																				   this.groupBox1});
			this.tabPage2.Location = new System.Drawing.Point(4, 22);
			this.tabPage2.Name = "tabPage2";
			this.tabPage2.Size = new System.Drawing.Size(412, 322);
			this.tabPage2.TabIndex = 1;
			this.tabPage2.Text = "tabPage2";
			// 
			// groupBox2
			// 
			this.groupBox2.BackColor = System.Drawing.SystemColors.Info;
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.label3});
			this.groupBox2.Location = new System.Drawing.Point(-4, -28);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(424, 76);
			this.groupBox2.TabIndex = 2;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "groupBox2";
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label3.Location = new System.Drawing.Point(16, 40);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(236, 24);
			this.label3.TabIndex = 5;
			this.label3.Text = "Welcome to RandomRat !";
			// 
			// groupBox4
			// 
			this.groupBox4.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.listRecentActions});
			this.groupBox4.Location = new System.Drawing.Point(17, 174);
			this.groupBox4.Name = "groupBox4";
			this.groupBox4.Size = new System.Drawing.Size(376, 136);
			this.groupBox4.TabIndex = 3;
			this.groupBox4.TabStop = false;
			this.groupBox4.Text = "Recent actions:";
			// 
			// listRecentActions
			// 
			this.listRecentActions.Items.AddRange(new object[] {
																   "GEN 80 Sets of \'This is a long name\'    >>    Print preview1",
																   "GEN 80 Sets of \'This is a long name\'    >>    Print preview2",
																   "GEN 80 Sets of \'This is a long name\'    >>    Print preview3",
																   "GEN 80 Sets of \'This is a long name\'    >>    Print preview4",
																   "GEN 80 Sets of \'This is a long name\'    >>    Print preview5",
																   "GEN 80 Sets of \'This is a long name\'    >>    Print preview6",
																   "GEN 80 Sets of \'This is a long name\'    >>    Print preview7"});
			this.listRecentActions.Location = new System.Drawing.Point(10, 18);
			this.listRecentActions.Name = "listRecentActions";
			this.listRecentActions.Size = new System.Drawing.Size(356, 108);
			this.listRecentActions.TabIndex = 0;
			this.listRecentActions.KeyDown += new System.Windows.Forms.KeyEventHandler(this.listRecentActions_KeyDown);
			this.listRecentActions.DoubleClick += new System.EventHandler(this.listRecentActions_DoubleClick);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.pictureBox1,
																					this.butNewCriteriaWizard,
																					this.butNewCriteria,
																					this.butLoadFromFile,
																					this.butGenNewSets});
			this.groupBox1.Location = new System.Drawing.Point(17, 58);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(376, 108);
			this.groupBox1.TabIndex = 1;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Options:";
			// 
			// pictureBox1
			// 
			this.pictureBox1.Image = ((System.Drawing.Bitmap)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(288, 31);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(72, 57);
			this.pictureBox1.TabIndex = 5;
			this.pictureBox1.TabStop = false;
			// 
			// butNewCriteriaWizard
			// 
			this.butNewCriteriaWizard.Location = new System.Drawing.Point(141, 76);
			this.butNewCriteriaWizard.Name = "butNewCriteriaWizard";
			this.butNewCriteriaWizard.Size = new System.Drawing.Size(132, 23);
			this.butNewCriteriaWizard.TabIndex = 3;
			this.butNewCriteriaWizard.Text = "New Criteria Wizard";
			this.butNewCriteriaWizard.Click += new System.EventHandler(this.butNewCriteriaWizard_Click);
			// 
			// butNewCriteria
			// 
			this.butNewCriteria.Location = new System.Drawing.Point(9, 76);
			this.butNewCriteria.Name = "butNewCriteria";
			this.butNewCriteria.Size = new System.Drawing.Size(128, 23);
			this.butNewCriteria.TabIndex = 2;
			this.butNewCriteria.Text = "New Criteria";
			this.butNewCriteria.Click += new System.EventHandler(this.butNewCriteria_Click);
			// 
			// butLoadFromFile
			// 
			this.butLoadFromFile.Enabled = false;
			this.butLoadFromFile.Location = new System.Drawing.Point(9, 48);
			this.butLoadFromFile.Name = "butLoadFromFile";
			this.butLoadFromFile.Size = new System.Drawing.Size(264, 23);
			this.butLoadFromFile.TabIndex = 1;
			this.butLoadFromFile.Text = "Load From File";
			this.butLoadFromFile.Click += new System.EventHandler(this.butLoadFromFile_Click);
			// 
			// butGenNewSets
			// 
			this.butGenNewSets.Location = new System.Drawing.Point(9, 20);
			this.butGenNewSets.Name = "butGenNewSets";
			this.butGenNewSets.Size = new System.Drawing.Size(264, 23);
			this.butGenNewSets.TabIndex = 0;
			this.butGenNewSets.Text = "Generate New Sets";
			this.butGenNewSets.Click += new System.EventHandler(this.butGenNewSets_Click);
			// 
			// tabPage3
			// 
			this.tabPage3.Controls.AddRange(new System.Windows.Forms.Control[] {
																				   this.label1,
																				   this.criteriaSummaryControl1,
																				   this.butNext,
																				   this.butBack1,
																				   this.groupBox6,
																				   this.txtSets,
																				   this.label4});
			this.tabPage3.Location = new System.Drawing.Point(4, 22);
			this.tabPage3.Name = "tabPage3";
			this.tabPage3.Size = new System.Drawing.Size(412, 322);
			this.tabPage3.TabIndex = 2;
			this.tabPage3.Text = "tabPage3";
			// 
			// label1
			// 
			this.label1.ForeColor = System.Drawing.Color.DarkRed;
			this.label1.Location = new System.Drawing.Point(148, 250);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(244, 16);
			this.label1.TabIndex = 33;
			this.label1.Text = "The criteria is invalid, please select another.";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			this.label1.Visible = false;
			// 
			// criteriaSummaryControl1
			// 
			this.criteriaSummaryControl1.Location = new System.Drawing.Point(8, 52);
			this.criteriaSummaryControl1.Name = "criteriaSummaryControl1";
			this.criteriaSummaryControl1.Size = new System.Drawing.Size(396, 192);
			this.criteriaSummaryControl1.TabIndex = 25;
			this.criteriaSummaryControl1.Action += new RandomRat.UserActionHandaler(this.criteriaSummaryControl1_Action);
			// 
			// butNext
			// 
			this.butNext.Location = new System.Drawing.Point(325, 288);
			this.butNext.Name = "butNext";
			this.butNext.TabIndex = 27;
			this.butNext.Text = "Next >";
			this.butNext.Click += new System.EventHandler(this.butNext_Click);
			// 
			// butBack1
			// 
			this.butBack1.Location = new System.Drawing.Point(8, 288);
			this.butBack1.Name = "butBack1";
			this.butBack1.TabIndex = 28;
			this.butBack1.Text = "< Back";
			this.butBack1.Click += new System.EventHandler(this.butBack1_Click);
			// 
			// groupBox6
			// 
			this.groupBox6.BackColor = System.Drawing.SystemColors.Info;
			this.groupBox6.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.label5});
			this.groupBox6.Location = new System.Drawing.Point(-4, -28);
			this.groupBox6.Name = "groupBox6";
			this.groupBox6.Size = new System.Drawing.Size(424, 76);
			this.groupBox6.TabIndex = 27;
			this.groupBox6.TabStop = false;
			this.groupBox6.Text = "groupBox6";
			// 
			// label5
			// 
			this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label5.Location = new System.Drawing.Point(16, 40);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(384, 24);
			this.label5.TabIndex = 5;
			this.label5.Text = "Select your criteria and sets to generate";
			// 
			// txtSets
			// 
			this.txtSets.Location = new System.Drawing.Point(100, 248);
			this.txtSets.MaxLength = 9;
			this.txtSets.Name = "txtSets";
			this.txtSets.Size = new System.Drawing.Size(44, 20);
			this.txtSets.TabIndex = 26;
			this.txtSets.Text = "80";
			this.txtSets.Leave += new System.EventHandler(this.txtSets_Leave);
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 252);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(92, 16);
			this.label4.TabIndex = 26;
			this.label4.Text = "Sets to generate:";
			// 
			// tabPage4
			// 
			this.tabPage4.Controls.AddRange(new System.Windows.Forms.Control[] {
																				   this.butAbort,
																				   this.progressBar1,
																				   this.butBack3,
																				   this.groupBox5,
																				   this.groupBox3});
			this.tabPage4.Location = new System.Drawing.Point(4, 22);
			this.tabPage4.Name = "tabPage4";
			this.tabPage4.Size = new System.Drawing.Size(412, 322);
			this.tabPage4.TabIndex = 3;
			this.tabPage4.Text = "tabPage4";
			// 
			// butAbort
			// 
			this.butAbort.Location = new System.Drawing.Point(325, 288);
			this.butAbort.Name = "butAbort";
			this.butAbort.TabIndex = 5;
			this.butAbort.Text = "Abort";
			this.butAbort.Click += new System.EventHandler(this.butAbort_Click);
			// 
			// progressBar1
			// 
			this.progressBar1.Location = new System.Drawing.Point(89, 289);
			this.progressBar1.Name = "progressBar1";
			this.progressBar1.Size = new System.Drawing.Size(229, 22);
			this.progressBar1.TabIndex = 31;
			// 
			// butBack3
			// 
			this.butBack3.Location = new System.Drawing.Point(8, 288);
			this.butBack3.Name = "butBack3";
			this.butBack3.TabIndex = 6;
			this.butBack3.Text = "< Back";
			this.butBack3.Click += new System.EventHandler(this.butBack3_Click);
			// 
			// groupBox5
			// 
			this.groupBox5.BackColor = System.Drawing.SystemColors.Info;
			this.groupBox5.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.label2});
			this.groupBox5.Location = new System.Drawing.Point(-4, -28);
			this.groupBox5.Name = "groupBox5";
			this.groupBox5.Size = new System.Drawing.Size(424, 76);
			this.groupBox5.TabIndex = 25;
			this.groupBox5.TabStop = false;
			this.groupBox5.Text = "groupBox5";
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Location = new System.Drawing.Point(16, 40);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(236, 24);
			this.label2.TabIndex = 5;
			this.label2.Text = "Select an action";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.butExport,
																					this.butCopyToClipboard,
																					this.butSaveAs,
																					this.butPrintPreview,
																					this.butPrint});
			this.groupBox3.Location = new System.Drawing.Point(117, 88);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(178, 168);
			this.groupBox3.TabIndex = 24;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Action:";
			// 
			// butExport
			// 
			this.butExport.Enabled = false;
			this.butExport.Location = new System.Drawing.Point(8, 135);
			this.butExport.Name = "butExport";
			this.butExport.Size = new System.Drawing.Size(160, 23);
			this.butExport.TabIndex = 4;
			this.butExport.Text = "Export";
			// 
			// butCopyToClipboard
			// 
			this.butCopyToClipboard.Location = new System.Drawing.Point(8, 106);
			this.butCopyToClipboard.Name = "butCopyToClipboard";
			this.butCopyToClipboard.Size = new System.Drawing.Size(160, 23);
			this.butCopyToClipboard.TabIndex = 3;
			this.butCopyToClipboard.Text = "Copy to Clipboard";
			this.butCopyToClipboard.Click += new System.EventHandler(this.butCopyToClipboard_Click);
			// 
			// butSaveAs
			// 
			this.butSaveAs.Location = new System.Drawing.Point(8, 77);
			this.butSaveAs.Name = "butSaveAs";
			this.butSaveAs.Size = new System.Drawing.Size(160, 23);
			this.butSaveAs.TabIndex = 2;
			this.butSaveAs.Text = "Save As";
			this.butSaveAs.Click += new System.EventHandler(this.butSaveAs_Click);
			// 
			// butPrintPreview
			// 
			this.butPrintPreview.Location = new System.Drawing.Point(8, 48);
			this.butPrintPreview.Name = "butPrintPreview";
			this.butPrintPreview.Size = new System.Drawing.Size(160, 23);
			this.butPrintPreview.TabIndex = 1;
			this.butPrintPreview.Text = "Print Preview";
			this.butPrintPreview.Click += new System.EventHandler(this.butPrintPreview_Click);
			// 
			// butPrint
			// 
			this.butPrint.Location = new System.Drawing.Point(8, 19);
			this.butPrint.Name = "butPrint";
			this.butPrint.Size = new System.Drawing.Size(160, 23);
			this.butPrint.TabIndex = 0;
			this.butPrint.Text = "Print";
			this.butPrint.Click += new System.EventHandler(this.butPrint_Click);
			// 
			// butCloseQuickStart
			// 
			this.butCloseQuickStart.Location = new System.Drawing.Point(184, 332);
			this.butCloseQuickStart.Name = "butCloseQuickStart";
			this.butCloseQuickStart.Size = new System.Drawing.Size(104, 23);
			this.butCloseQuickStart.TabIndex = 1;
			this.butCloseQuickStart.Text = "Close Quick Start";
			this.butCloseQuickStart.Click += new System.EventHandler(this.butCloseQuickStart_Click);
			// 
			// chkShowOnStartUp
			// 
			this.chkShowOnStartUp.Checked = true;
			this.chkShowOnStartUp.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chkShowOnStartUp.Location = new System.Drawing.Point(8, 332);
			this.chkShowOnStartUp.Name = "chkShowOnStartUp";
			this.chkShowOnStartUp.Size = new System.Drawing.Size(164, 24);
			this.chkShowOnStartUp.TabIndex = 4;
			this.chkShowOnStartUp.Text = "Show Quick Start on startup";
			this.chkShowOnStartUp.CheckedChanged += new System.EventHandler(this.chkShowOnStartUp_CheckedChanged);
			// 
			// butExitRandomRat
			// 
			this.butExitRandomRat.Location = new System.Drawing.Point(296, 332);
			this.butExitRandomRat.Name = "butExitRandomRat";
			this.butExitRandomRat.Size = new System.Drawing.Size(104, 23);
			this.butExitRandomRat.TabIndex = 2;
			this.butExitRandomRat.Text = "Exit RandomRat";
			this.butExitRandomRat.Click += new System.EventHandler(this.butExitRandomRat_Click);
			// 
			// QuickStart
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(408, 362);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.butExitRandomRat,
																		  this.chkShowOnStartUp,
																		  this.butCloseQuickStart,
																		  this.tabControl1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "QuickStart";
			this.ShowInTaskbar = false;
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "RandomRat - Quick Start";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.QuickStart_Closing);
			this.tabControl1.ResumeLayout(false);
			this.tabPage2.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox4.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.tabPage3.ResumeLayout(false);
			this.groupBox6.ResumeLayout(false);
			this.tabPage4.ResumeLayout(false);
			this.groupBox5.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion
		#endregion

		#region NAV Butons

		private void butGenNewSets_Click(object sender, System.EventArgs e) {
			tabControl1.SelectedIndex++;
		}

		private void butBack1_Click(object sender, System.EventArgs e) {
			tabControl1.SelectedIndex--;
		}


		private void butBack3_Click(object sender, System.EventArgs e) {
			tabControl1.SelectedIndex--;
		}

		private void pageBack() {

		}

		#endregion

		#region Exiting Buttons

		private void butExitRandomRat_Click(object sender, System.EventArgs e) {
			_action = QuickStartAction.ExitRandomRat;
			this.Close();
		}

		private void butCloseQuickStart_Click(object sender, System.EventArgs e) {
			_action = QuickStartAction.CloseQuickStart;
			this.Close();
		}

		private void butLoadFromFile_Click(object sender, System.EventArgs e) {
			_action = QuickStartAction.LoadFromFile;
			this.Close();
		}

		private void butNewCriteria_Click(object sender, System.EventArgs e) {
			_action = QuickStartAction.NewCriteria;
			this.Close();
		}

		private void butNewCriteriaWizard_Click(object sender, System.EventArgs e) {
			_action = QuickStartAction.NewCriteriaWizard;
			this.Close();
		}

		#endregion

		private void QuickStart_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
			if (workerThread != null && workerThread.IsAlive) {
				workerThread.Abort();
			}
		}

		private void butPrint_Click(object sender, System.EventArgs e) {
			printSets();
		}

		private void butPrintPreview_Click(object sender, System.EventArgs e) {
			printPreviewSets();
		}
		private void butSaveAs_Click(object sender, System.EventArgs e) {
			saveSetsAs();
		}
		private void butCopyToClipboard_Click(object sender, System.EventArgs e) {
			copyToClip();
		}

		private void printSets() {
			RandomSetPrinter setPrinter = new RandomSetPrinter(rndSets, setArgs, this);
			
			if (setPrinter.Print(RandomSetPrintMode.PrintWithDiolog, this)) {
				
				addActionItem(new QuickStartActionItem(QuickStartActionSource.Generate,
					criteriaID,
					rndSets.Count, QuickStartSetAction.Print));
			}
		}

		private void printPreviewSets() {
			RandomSetPrinter setPrinter = new RandomSetPrinter(rndSets, setArgs, this);

            if (setPrinter.Print(RandomSetPrintMode.PrintPreview, this))
            {
				
				addActionItem(new QuickStartActionItem(QuickStartActionSource.Generate,
					criteriaID,
					rndSets.Count, 
					QuickStartSetAction.PrintPreview));
			}
		}

		private void saveSetsAs() {
			if (DataManager.SaveSetsToFile(rndSets, formState, SetsFileType.Multiple)) {
				addActionItem(new QuickStartActionItem(QuickStartActionSource.Generate,
					criteriaID,
					rndSets.Count, 
					QuickStartSetAction.SaveSetsAs));
			}
		}

		private void copyToClip() {
			DataManager.CopySetsToClipboard(rndSets, setArgs);

			addActionItem(new QuickStartActionItem(QuickStartActionSource.Generate,
				criteriaID,
				rndSets.Count, 
				QuickStartSetAction.CopyToClipboard));

			MessageBox.Show(this, "The sets have been copied to the clipboard.");
		}

		private void listRecentActions_DoubleClick(object sender, System.EventArgs e) {
			DoActionItem((QuickStartActionItem)listRecentActions.SelectedItem);
		}
		private void listRecentActions_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
			if (e.KeyCode == Keys.Enter) DoActionItem((QuickStartActionItem)listRecentActions.SelectedItem);
		}


		private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e) {
			
			if (tabControl1.SelectedIndex == 0) {
				butGenNewSets.Enabled = criteriaSummaryControl1.listBox1.Items.Count != 0;
				fillActionList();
			} else if (tabControl1.SelectedIndex == 1) {

			}
		}

		private void chkShowOnStartUp_CheckedChanged(object sender, System.EventArgs e) {
			ApplicationSettings.ShowQuickStart = chkShowOnStartUp.Checked;
		}

		private void tabControl1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
			if (e.KeyCode == Keys.Left) {
				chkShowOnStartUp.Focus();
				e.Handled = true;
			} else if (e.KeyCode == Keys.Right) {
				butCloseQuickStart.Focus();
				e.Handled = true;
			}
		}



	}

    public delegate void UIWork();

    public enum QuickStartActionSource { Generate, NotApplicable };
    public enum QuickStartSetAction { Print, PrintPreview, SaveSetsAs, CopyToClipboard };

    public enum QuickStartAction { ExitRandomRat, NewCriteria, NewCriteriaWizard, LoadFromFile, CloseQuickStart }


    public class QuickStartActionItem
    {
        public QuickStartActionSource Source;
        public string CriteriaID;
        public int Sets;
        public QuickStartSetAction Action;

        public QuickStartActionItem(QuickStartActionSource Source, string CriteriaID, int Sets, QuickStartSetAction Action)
        {
            this.Source = Source;
            this.CriteriaID = CriteriaID;
            this.Sets = Sets;
            this.Action = Action;
        }
        public QuickStartActionItem(QuickStartActionSource Source, QuickStartSetAction Action)
        {
            this.Source = Source;
            this.Sets = -1;
            this.Action = Action;
        }
        public QuickStartActionItem(QuickStartSetAction Action)
        {
            this.Source = QuickStartActionSource.NotApplicable;
            this.Sets = -1;
            this.Action = Action;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder(75);

            if (Source == QuickStartActionSource.Generate)
            {
                //GEN 80 Sets of 'This is a long name'    >>    Print preview1
                sb.Append("GEN " + Sets.ToString() + " Sets of  '" + CriteriaID + "'    >>    ");

                if (Action == QuickStartSetAction.Print)
                {
                    sb.Append("Print");

                }
                else if (Action == QuickStartSetAction.PrintPreview)
                {
                    sb.Append("Print Preview");

                }
                else if (Action == QuickStartSetAction.SaveSetsAs)
                {
                    sb.Append("Save As...");
                }
                else if (Action == QuickStartSetAction.CopyToClipboard)
                {
                    sb.Append("Copy to clipboard");
                }
            }

            return sb.ToString();
        }

    }


}
